/* 
 * %W% %E% Enrique Madrid
 *
 * Copyright (c) 1975-2008 M&T Consulting, Inc. All rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
*/

package com.myt.tools.obtk;

// Import Openbravo packages
import com.myt.tools.obtk.openbravo.account.*;

// Import M&T packages
import com.myt.util.att.*;

// Import Java packages


public class CmdCsv2Xml extends AttApp {

    private String inp_filename = "";
    private String out_filename = "";

    public CmdCsv2Xml(String name) {
	// Calling Base class constructor.
	super(name);
	// Setting options supported
	acceptOption("i", "Openbravo's chart of accounts in comma separated values format.",
		     AttApp.ATP_OPT_REQUIRED | AttApp.ATP_ARG_REQUIRED, "source.csv");
	acceptOption("o", "Hierarquized Openbravo's chart of accounts file in XML format.",
		     AttApp.ATP_OPT_REQUIRED | AttApp.ATP_ARG_REQUIRED, "targe.xml");
	acceptOption("k", "Reset primary key value using <key> period.", AttApp.ATP_ARG_REQUIRED , "key");
    }

    public void exec(String[] args) {

	if (!parseOptions(args) || hasOption("h")) {
	    printHelp(getName());
	} else {

	    Chart chartOfAccounts = Chart.newInstance();
	    if (chartOfAccounts != null) {
		if (chartOfAccounts.load(inp_filename, Chart.CHA_FMT_CSV)) {
		    chartOfAccounts.saveAs(out_filename, Chart.CHA_FMT_XML);
		}
	    } else {
		System.out.println("Imposible create Chart Of Accounts!");
	    }

	}

    }

    protected boolean setOptions() {
	boolean ret = false;
	if (hasOption("i")) {
	    inp_filename = argumentOf("i");
	    if (hasOption("o")) {
		out_filename = argumentOf("o");
		ret = true;
	    }
	}

	return ret;
    }

}